package server.commonData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import server.ScriptEnum;
import server.ServerEngin;
import server.business.play.script.IPlayGameScript;
import server.commonData.interfaces.IUpdate;
import server.msgBean.LoginMessage.TableInfoRes;

/**
 * 桌子
 * @author King
 *
 */
public class Table implements IUpdate
{
	/**桌子id**/
	private long id;
	/**房间**/
	private Room room;
	/**打牌的玩家**/
	private Map<Long, PokerPlayer> players = new HashMap<Long, PokerPlayer>();
	/**打牌状态**/
	private int status = ScriptEnum.PLAY_WAIT;
	/**庄玩家id**/
	private long zhuangPlayerId;
	/**选牌剩余时间**/
	private long xuanpaiEndtime;
	/**准备剩余时间**/
	private long zunbeiEndtime;
	/**桌子是否等待移除**/
	private boolean isOver;
	
	
	
	/**
	 * 玩家人数上上限
	 */
	private int userMaxNum = 2;
	
	public Table(long id,Room room)
	{
		this.id = id;
		this.room = room;
	}
	
	/**
	 * 设置桌子玩家人数上限
	 * @param userMaxNum
	 */
	public void setTableUserMaxNum(int userMaxNum)
	{
		this.userMaxNum =userMaxNum;	
	}
	
	/**
	 * 桌子是否满了
	 * @return
	 */
	public boolean isFull()
	{
		return players.size()==this.userMaxNum;
	}
	
	/**增加桌子的玩家**/
	public void addPlayer(Player player)
	{
		player.setRoomName(this.room.getRoomName());
		player.setTableId(this.id);
		PokerPlayer pokerPlayer = new PokerPlayer(player);
		this.players.put(player.getId(), pokerPlayer);
	}
	
	/**删除桌子的玩家**/
	public void delPlayer(Player player)
	{
		player.setTableId(0);
		this.players.remove(player.getId());
	}
	
	@Override
	public void onTime(long curMill)
	{
		IPlayGameScript script = ServerEngin.getInstance().getScriptM().getScript(this.status);
		script.onUpdate(this, curMill);
	}
	
	
	
	
	/**
	 * 桌子是否空了
	 * @return
	 */
	public boolean isEmpty()
	{
		return players.isEmpty();
	}
	
	/**房间结束了**/
	public void over()
	{
		
		this.isOver = true;
	}
	
	/**
	 * 获得庄
	 * @return
	 */
	public PokerPlayer getZhuang()
	{
		return this.players.get(zhuangPlayerId);
	}
	
	/**
	 * 显示桌子信息给前端
	 * @return
	 */
	public TableInfoRes.Builder showTableInfo()
	{
		TableInfoRes.Builder builder = TableInfoRes.newBuilder();
		for(PokerPlayer player:this.players.values())
		{
			builder.addUserInfo(player.getPlayer().showTablePlayerInfo());
		}
		builder.setPlayStep(this.status);
		return builder;
	}

	
	public long getId() {
		return id;
	}

	public Map<Long, PokerPlayer> getPlayers() {
		return players;
	}
	
	public Map<Long, PokerPlayer> getPlayingPlayers()
	{
		Map<Long, PokerPlayer> map = new HashMap<Long, PokerPlayer>();
		for(PokerPlayer player:this.players.values())
		{
			if(!player.isPlay())
				continue;
			map.put(player.getPlayer().getId(), player);
		}
		return map;
	}
	

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	/**
	 * 获得桌上在线的玩家
	 * @return
	 */
	public List<User> getUsers()
	{
		List<User> users = new ArrayList<User>();
		for(PokerPlayer player:this.players.values())
		{
			if(player.getPlayer().getType()!=Player.USER)
				continue;
			users.add((User)player.getPlayer());
		}
		return users;
	}

	public long getZhuangPlayerId() {
		return zhuangPlayerId;
	}

	public void setZhuangPlayerId(long zhuangPlayerId) {
		this.zhuangPlayerId = zhuangPlayerId;
	}

	public long getXuanpaiEndtime() {
		return xuanpaiEndtime;
	}

	public void setXuanpaiEndtime(long xuanpaiEndtime) {
		this.xuanpaiEndtime = xuanpaiEndtime;
	}

	public Room getRoom() {
		return room;
	}

	public int getUserMaxNum() {
		return userMaxNum;
	}

	public long getZunbeiEndtime() {
		return zunbeiEndtime;
	}

	public void setZunbeiEndtime(long zunbeiEndtime) {
		this.zunbeiEndtime = zunbeiEndtime;
	}

	public boolean isOver() {
		return isOver;
	}

}
